package com.tianhui.consignortwofei.other.auth.state;

import android.content.Context;

import com.fgs.common.entity.PictureInfo;
import com.fgs.common.entity.UploadPictureInfo;
import com.fgs.common.uploadPicture.IUploadPicture;
import com.fgs.common.uploadPicture.UploadPicture;
import com.tianhui.consignortwofei.mvp.model.enty.RecognitionInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.other.auth.inte.ProcessCallback;
import com.tianhui.consignortwofei.other.auth.inte.RecognitionCallback;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.module.IPictureModule;
import com.tianhui.consignortwofei.other.auth.ocr.IOcrRecognition;
import com.tianhui.consignortwofei.other.auth.recognition.process.IRecognitionProcess;

/**
 * @author fgs
 * @date 2021/5/13
 * @description 识别并上传
 **/
public class RecognitionAndUploadState<T> implements IPictureProcessState {
    private final IOcrRecognition iOcrRecognition;
    private final IRecognitionProcess iRecognitionProcess;
    private final IPictureModule pictureModule;

    public RecognitionAndUploadState(IOcrRecognition iOcrRecognition, IRecognitionProcess iRecognitionProcess, IPictureModule pictureModule) {
        this.iOcrRecognition = iOcrRecognition;
        this.iRecognitionProcess = iRecognitionProcess;
        this.pictureModule = pictureModule;
    }

    private void uploadPicture(Context context, SelectPhotoInfo selectPhotoInfo, T t, SelectPhotoCallback<T> callback) {
        UploadPictureInfo uploadPictureInfo = new UploadPictureInfo();
        uploadPictureInfo.picturePath = selectPhotoInfo.filePath;
        uploadPictureInfo.pictureModule = pictureModule.pictureModule();
        new UploadPicture(new IUploadPicture.UploadPictureCallback() {
            @Override
            public void onSuccess(UploadPictureInfo uploadPictureInfo, PictureInfo pictureInfo) {
                selectPhotoInfo.pictureId = pictureInfo.id;
                selectPhotoInfo.realFileName = pictureInfo.realFileName;
                selectPhotoInfo.resultInfo = t;
                callback.selectPhotoSuccess(selectPhotoInfo);
            }

            @Override
            public void onFailed(UploadPictureInfo uploadPictureInfo) {

            }
        }).uploadPicture(context, uploadPictureInfo);
    }

    @Override
    public void stateProcess(Context context, SelectPhotoInfo selectPhotoInfo, SelectPhotoCallback callback) {
        iOcrRecognition.recognition().recognition(context, selectPhotoInfo, new RecognitionCallback() {
            @Override
            public void recognitionSuccess(RecognitionInfo recognitionInfo) {
                iRecognitionProcess.recognitionProcess(recognitionInfo, selectPhotoInfo, new ProcessCallback<T>() {
                    @Override
                    public void processSuccess(T t) {
                        uploadPicture(context, selectPhotoInfo, t, callback);
                    }

                    @Override
                    public void processFailed() {
                        uploadPicture(context, selectPhotoInfo, null, callback);
//                        callback.selectPhotoFailed();
                    }
                });
            }

            @Override
            public void recognitionFailed() {
//                callback.selectPhotoFailed();
                uploadPicture(context, selectPhotoInfo, null, callback);
            }
        });
    }
}
